package key;


// EncryptClasses.java

import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
/**
 * 加密
 * @author Administrator
 *
 */
public class EncryptClasses
{
  static public void main( String args[] ) throws Exception {
	  args=new String[5];
	  args[0]="key.data";
	  args[1]="F:/Java/workspaces/9/javaTest/bin/key/App.class";
	  args[2]="F:/Java/workspaces/9/javaTest/bin/key/Bar.class";
	  args[3]="F:/Java/workspaces/9/javaTest/bin/App.class";
	  args[4]="F:/Java/workspaces/9/javaTest/bin/Bar.class";
    String keyFilename = args[0];
    String algorithm = "DES";

    // 生成密匙
    SecureRandom sr = new SecureRandom();
    byte rawKey[] = Util.readFile( keyFilename );
    DESKeySpec dks = new DESKeySpec( rawKey );
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance( algorithm );
    SecretKey key = keyFactory.generateSecret( dks );

    // 创建用于实际加密操作的Cipher对象
    Cipher ecipher = Cipher.getInstance( algorithm );
    ecipher.init( Cipher.ENCRYPT_MODE, key, sr );

    // 加密命令行中指定的每一个类
    for (int i=1; i<args.length; ++i) {
    	try{
      String filename = args[i];

      // 读入类文件
      byte classData[]=null;
                     
                     classData = Util.readFile( filename );
                     

      // 加密
      byte encryptedClassData[] = ecipher.doFinal( classData );

      // 保存加密后的内容
      Util.writeFile( filename, encryptedClassData );

      System.out.println( "Encrypted "+filename );}
      catch(Exception e){
     	 e.printStackTrace();
      }
    }
  }
}